package com.orifine.sql.simple;

import java.util.Map;

public class SimpleBuilderUtils {
	private static final String SORT = "sort";
	private static final String PAGE = "page";
	private static final String KEY = "key";
	private static final String VALUE = "value";
	
	public static SimpleSelectBuilder sort(SimpleSelectBuilder selectBuilder, Map<String, Object> sqlParam){
		SimpleSort bowosSort = (SimpleSort) sqlParam.get(SORT);
		sort(selectBuilder, bowosSort);
		return selectBuilder;
	}
	
	public static SimpleSelectBuilder page(SimpleSelectBuilder selectBuilder, Map<String, Object> sqlParam){
		SimplePage bowosPage = (SimplePage) sqlParam.get(PAGE);
		page(selectBuilder, bowosPage);
		return selectBuilder;
	}

	public static SimpleSelectBuilder sort(SimpleSelectBuilder selectBuilder, SimpleSort bowosSort) {
		if(bowosSort != null){
			for(Map<String, String> entity:bowosSort.getEntry()){
				selectBuilder.orderBy(entity.get(KEY)+" "+entity.get(VALUE));
			}
		}
		return selectBuilder;
	}
	
	public static SimpleSelectBuilder page(SimpleSelectBuilder selectBuilder, SimplePage bowosPage){
		if(bowosPage != null){
			selectBuilder.limit(bowosPage.getListFirstIndex(), bowosPage.getListMax());
		}
		return selectBuilder;
	}
}
